﻿using UnityEngine;
using System.Collections;

using System;

/// <summary>
/// 次方案目前仅用于Android //
/// Input manager.
/// </summary>
public class InputManager : MonoBehaviour
{
#if UNITY_ANDROID
	public bool isCanInput = true;	// 用这个来控制所有控制输入 //

	public bool isPressMenuButtonDown = false;	// 因为每次按下只做一次处理 //
	public Action pressMenuButtonDownHandle;	//  菜单键的按下委托 //
	public Action menuButtonOnClicknHandle;	//  菜单键的点击委托 //

	public bool isPressEscapeButtonDown = false;	// 因为每次按下只做一次处理 //
	public Action pressEscapeButtonDownHandle; //  返回键的按下委托 //
	public Action escapeButtonOnClickHandle; //  返回键的点击委托 //

	/// <summary>
	/// 无论什么时候, 点击按键肯定要保存数据 //
	/// !!!记得InputManager初始化要在GameDataManager初始化之后
	/// Init this instance.
	/// </summary>
	public void Init()
	{
		pressMenuButtonDownHandle += GameDataManager.Instance.saveDatasHandle;
		pressEscapeButtonDownHandle += GameDataManager.Instance.saveDatasHandle;
	}

	void Update ()
	{
		if(!isCanInput)
		{
			return;
		}

		// ---------------------------------------------- 菜单键的按下的处理 ---------------------------------------------- //
		if(Input.GetKeyDown(KeyCode.Menu) && !isPressMenuButtonDown)
		{
			isPressMenuButtonDown = true;
			if(pressMenuButtonDownHandle != null)
			{
				pressMenuButtonDownHandle();
			}
		}

		if(Input.GetKeyUp(KeyCode.Menu))
		{
			isPressMenuButtonDown = false;
			if(menuButtonOnClicknHandle != null)
			{
				menuButtonOnClicknHandle();
				isCanInput = false;
			}
		}

		// ---------------------------------------------- 返回键键的按下的处理 ---------------------------------------------- //
		if(Input.GetKeyDown(KeyCode.Escape) && !isPressEscapeButtonDown)
		{
			isPressEscapeButtonDown = true;
			if(pressEscapeButtonDownHandle != null)
			{
				pressEscapeButtonDownHandle();
			}
		}

		if(Input.GetKeyUp(KeyCode.Escape))
		{
			isPressEscapeButtonDown = false;
			if(escapeButtonOnClickHandle != null)
			{
				escapeButtonOnClickHandle();
				isCanInput = false;
			}
		}
	}

	#region ---------------------------------------------- Singlestone ----------------------------------------------
	private static InputManager instance;
	public static InputManager Instance
	{
		get
		{
			if (instance == null)
			{
				GameObject go = new GameObject("InputManager", typeof(InputManager));
				instance = go.GetComponent<InputManager>();
				DontDestroyOnLoad(go);
			}
			
			return instance;
		}
	}
	#endregion
#endif
}
